package com.bmf.admin.module.attribute.controller;

import com.bmf.admin.constant.ChannelConstant;
import com.bmf.admin.module.attribute.entity.Attribute;
import com.bmf.admin.module.attribute.service.AttributeService;
import com.bmf.core.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * (Attribute)表控制层
 *
 * @author makejava
 * @since 2022-11-04 17:32:50
 */
@RestController
@RequestMapping("attribute")
@Api(tags = {"属性接口"})
@AllArgsConstructor
public class AttributeController{
    /**
     * 服务对象
     */
    private AttributeService attributeService;

//    private RedisTemplate<Object,Object> redisTemplate;

//    private final RedisUtils redisUtils;
    /**
     * 发布广播消息
     *
     * @param msg
     */
    @GetMapping(value = "/publish/{msg}/{msg2}")
    public void sendMsg(@PathVariable(value = "msg") String msg,@PathVariable(value = "msg2") String msg2) {
//        redisUtils.set("publish:msg", msg);
//        redisTemplate.convertAndSend(ChannelConstant.CHANNEL_GLOBAL_NAME, msg);
//        redisTemplate.convertAndSend(ChannelConstant.CHANNEL_SINGLE_NAME,msg2);
    }

    /**
     * 分页查询所有数据
     *
     * @param attribute 分页对象
     * @return 所有数据
     */
    @PostMapping("/selectPage")
    @ApiOperation(value = "分页查询所有数据")
    public R selectPage(@RequestBody Attribute attribute) {
        return R.success(attributeService.selectPage(attribute));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "通过主键查询单条数据")
    public R selectOne(@PathVariable("id") Integer id) {
        return R.success(attributeService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param attribute 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增数据")
    public R insert(@RequestBody Attribute attribute) {
        boolean save = attributeService.save(attribute);
        if(save){
            return R.success(attribute.getId());
        }
        return R.failed();
    }

    /**
     * 修改数据
     *
     * @param attribute 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改数据")
    public R update(@RequestBody Attribute attribute) {
        boolean b = attributeService.updateById(attribute);
        if(b){
            return R.ok();
        }
        return R.failed();
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation(value = "删除数据")
    public R delete(@RequestParam("ids") List<Integer> ids) {
        boolean b = attributeService.removeBatchByIds(ids);
        if(b){
            return R.ok();
        }
        return R.failed();
    }
}

